package com.hbx.controller;


import com.hbx.dto.BaseRequest;
import com.hbx.dto.resource.RecycleDto;
import com.hbx.service.RecycleBinService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-27
 */
@RestController
@RequestMapping("/recycle")
@Api(tags = "回收站处理器")
public class RecycleBinController {

    @Autowired
    private RecycleBinService recycleBinService;

    /**
     * 管理员删除资源
     * @param dto
     * @param result
     * @return
     */
    @RequestMapping(value = "/addRecycle",method = RequestMethod.POST)
    @ApiOperation("删除文件")
    public Result addRecycle(@RequestBody @Valid RecycleDto dto, BindingResult result){
        Result r = new Result();
        if (ObjectUtils.isEmpty(result.hasErrors())){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        recycleBinService.addRecycle(dto, r);
        return r;
    }

    /**
     * 获得回收站列表
     * @return
     */
    @RequestMapping(value = "/recycleList",method = RequestMethod.GET)
    @ApiOperation("获得回收站列表")
    public Result recycleList(BaseRequest request){
        Result r = new Result();
        if (request.getPageNum() == null){
            r.setCode(400).setMsg("pageNum为必传项");
            return r;
        }
        if (request.getPageSize() == null){
            r.setCode(400).setMsg("pageSize为必传项");
            return r;
        }
        recycleBinService.recycleList(request, r);
        return r;
    }

    /**
     * 永久删除文件
     * @return
     */
    @RequestMapping(value = "/recycleRemove",method = RequestMethod.GET)
    @ApiOperation("永久删除文件")
    public Result recycleRemove(@RequestParam("id") Integer id){
        Result r = new Result();
        if (id == null){
            r.setCode(400).setMsg("id为必传项");
            return r;
        }
        recycleBinService.recycleRemove(id, r);
        return r;
    }

    /**
     * 恢复文件
     * @return
     */
    @RequestMapping(value = "/recycleRecovery",method = RequestMethod.GET)
    @ApiOperation("恢复文件")
    public Result recycleRecovery(@RequestParam("id") Integer id){
        Result r = new Result();
        if (id == null){
            r.setCode(400).setMsg("id为必传项");
            return r;
        }
        recycleBinService.recycleRecovery(id, r);
        return r;
    }

    /**
     * 清空回收站
     * @return
     */
    @RequestMapping(value = "/recycleClear",method = RequestMethod.GET)
    @ApiOperation("清空回收站")
    public Result recycleClear(){
        Result r = new Result();
        recycleBinService.recycleClear(r);
        return r;
    }

    /**
     * 获取回收站详情
     * @return
     */
    @RequestMapping(value = "/getRecycleInfo",method = RequestMethod.GET)
    @ApiOperation("获取回收站详情")
    public Result getRecycleInfo(@RequestParam("id") Integer id){
        Result r = new Result();
        if (id == null) {
            r.setCode(400).setMsg("id为必传项");
            return r;
        }
        recycleBinService.getRecycleInfo(id, r);
        return r;
    }
}

